package org.dromara.oce.domain.req;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.dromara.oce.utils.CommonConstants;
import org.dromara.oce.utils.DateTimeUtil;
import org.dromara.oce.utils.PlatformConstants;
import org.dromara.oce.utils.PlatformParamTransUtil;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;


/**
 * @desc:交易订单表
 * @autor: hhl
 * @date: 2024-04-08 17:23:55<br>
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class TradeOrder extends Model<TradeOrder> {
    /**
     * 支付状态：-1-异常；0-待支付；1-已支付；2-支付失败；3-已退款；4-已关闭
     */
    public static final Integer STATUS_PAY_EXCEPTION = -1;
    public static final Integer STATUS_PAY_WAIT = 0;
    public static final Integer STATUS_PAY_SUCCESS = 1;
    public static final Integer STATUS_PAY_FAIL = 2;
    public static final Integer STATUS_PAY_REFUND = 3;
    public static final Integer STATUS_PAY_CLOSE = 4;
    /**
     * 第三方交易状态：NOTPAY-未支付；SUCCESS-支付成功；FAILED-失败；REFUND-转入退款；USERPAYING-用户支付中；
     */
    public static final String TRADE_STATE_NOTPAY = "NOTPAY";
    public static final String TRADE_STATE_SUCCESS = "SUCCESS";
    public static final String TRADE_STATE_FAILED = "FAILED";
    public static final String TRADE_STATE_REFUND = "REFUND";
    public static final String TRADE_STATE_USERPAYING = "USERPAYING";
    public static final String TRADE_STATE_TIMEOUT = "TIMEOUT";

    /**
     * 商品类型：1-充值虚拟币；2-购买会员；3-购买剧集；4-购买全集；10-虚拟币解锁单集；11-分享解锁；12-权益券解锁；13-广告解锁；
     */
    public static final Integer ORDER_TYPE_COINS = 1;
    public static final Integer ORDER_TYPE_VIP = 2;
    public static final Integer ORDER_TYPE_EPISODE = 3;
    public static final Integer ORDER_TYPE_ALL_EPISODE = 4;

    public static final String PAY_CHANNEL_UNKNOWN = "UNKNOWN";
    public static final String PAY_CHANNEL_WECHAT = "WECHAT";
    public static final String PAY_CHANNEL_ALIPAY = "ALIPAY";
    public static final String PAY_CHANNEL_DOUYIN = "DOUYIN";
    /**
     * 抖音-钻石支付
     */
    public static final String PAY_CHANNEL_DOUYIN_DIAMOND = "DOUYIN-DIAMOND";

    public static final Integer IS_PUSH_OCE_NO = 0;
    public static final Integer IS_PUSH_OCE_YES = 1;

    //@ApiModelProperty(value = "订单id")
    @TableId(type = IdType.AUTO)
    private Long orderId;

    //@ApiModelProperty(value = "app用户id")
    private Long userId;

    //@ApiModelProperty(value = "订单号")
    private String outTradeNo;

    //@ApiModelProperty(value = "总金额")
    private BigDecimal totalAmount;

    //@ApiModelProperty("下单平台：mp-kuaishou-快手；mp-toutiao-抖音；mp-weixin-微信；")
    private String orderPlatform;

    //@ApiModelProperty(value = "交易时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime tradeTime;

    //@ApiModelProperty(value = "推广链接id")
    private Long referralLinkId;

    //@ApiModelProperty("用户设备系统平台：andorid-安卓；ios-苹果；")
    private String devicePlatform;

    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    //@ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;

    //@ApiModelProperty(value = "此推广链接是否首次付费：-1-未计算；0-首次；1-非首次；")
    private Integer isFirstRecharge;

    //@ApiModelProperty(value = "第三方广告主id")
    private String thirdAdvertiserId;
    //@ApiModelProperty("巨量广告体验版的项目ID")
    private String projectId;
    //@ApiModelProperty("投广广告id")
    private String promotionId;

    //@ApiModelProperty("推广链接，当天注册用户是否首次付费：-1-未计算；0-首次；1-非首次；")
    private Integer thatdayNewUserFirstRecharge;
    //@ApiModelProperty("推广链接下，当日用户是否首次付费：-1-未计算；0-首次；1-非首次；")
    private Integer thatdayUserFirstRecharge;
    //@ApiModelProperty("推广链接下，当日复充计数标识：-1-未计算；0-不记数；1-记数；")
    private Integer thatdayRepeatRechargeFlag;
    //@ApiModelProperty("推广链接，复充计数标识：-1-未计算；0-不记数；1-记数；")
    private Integer repeatRechargeFlag;
    //@ApiModelProperty("平台编码")
    private String appCode;

    //@ApiModelProperty("当天时间戳（yyyy-MM-dd）")
    private Long dateTimestamp;
    //@ApiModelProperty("交易时间戳")
    private Long tradeTimestamp;

    //@ApiModelProperty("用户最后链接染色时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastReferralLinkTime;

    /**
     * 转为对应平台的数字：平台类型：1-抖音；2-快手；3-微信；
     *
     * @return
     */
    public Integer toPlatformNum() {
        if (PlatformConstants.DOUYIN.equals(this.orderPlatform)) {
            return 1;
        } else if (PlatformConstants.KUAISHOU.equals(this.orderPlatform)) {
            return 2;
        } else if (PlatformConstants.WECHAT.equals(this.orderPlatform)) {
            return 3;
        } else {
            return -1;
        }
    }

    public static Integer toPlatformNum(String orderPlatform) {
        if (PlatformConstants.DOUYIN.equals(orderPlatform)) {
            return 1;
        } else if (PlatformConstants.KUAISHOU.equals(orderPlatform)) {
            return 2;
        } else if (PlatformConstants.WECHAT.equals(orderPlatform)) {
            return 3;
        } else {
            return -1;
        }
    }


    /**
     * 判断是否是首充
     */
    public boolean isFirstRecharge() {
        return CommonConstants.IS_FIRST_RECHARGE_YES == this.isFirstRecharge;
    }

    /**
     * 判断是否当日注册用户首充
     * @return
     */
    public boolean isNewUserFirstRecharge(){
        if(CommonConstants.IS_FIRST_RECHARGE_YES == this.thatdayNewUserFirstRecharge){
            return true;
        }
        return false;
    }

    /**
     * 判断订单是不是ios支付
     * @return
     */
    public boolean isIosPay(){
        return CommonConstants.DEVICE_PLATFORM_IOS.equals(this.devicePlatform);
    }

//    /**
//     * 计算平台的分成金额
//     * @return
//     */
//    public BigDecimal computeOrderAmount() {
//        //袋鼠   微小  0.9个点，抖小0.99   利润  = 分成 - 消耗
//        if (PlatformParamTransUtil.isDouYin(this.orderPlatform)) {
//            return totalAmount.multiply(new BigDecimal("0.99")).setScale(4, RoundingMode.HALF_UP);
//        } else if (PlatformParamTransUtil.isKuaiShou(this.orderPlatform)) {
//
//        } else if (PlatformParamTransUtil.isWechat(this.orderPlatform)) {
//            return totalAmount.multiply(new BigDecimal("0.9")).setScale(4,RoundingMode.HALF_UP);
//        }
//        return BigDecimal.ZERO;
//    }

    /**
     * 判断订单是否存在推广链接id
     * @return
     */
    public boolean isExistReferralLinkId(){
        return !ObjectUtils.isEmpty(this.referralLinkId) && this.referralLinkId > 0;
    }

    /**
     * 获取用户最后染色时间戳
     * @return
     */
    public Long getLastUserReferralTimestamp(){
        if(!ObjectUtils.isEmpty(this.lastReferralLinkTime)){
            return DateTimeUtil.getTimestampSeconds(this.lastReferralLinkTime);
        }
        return DateTimeUtil.getTimestampSeconds(this.createTime);
    }
}
